﻿using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using System.Linq;

namespace ExtensionMethodExtravaganza.Tests
{
	[TestFixture]
	public class IEnumerableExtensionsTests
	{
		[Test]
		public void ForEachTest()
		{
			var employees = new List<Employee> { new Employee() };
			var managers = new List<Manager> { new Manager() };
			var customers = new List<Customer> { new Customer() };

			var everyone = new List<Person>();

			IEnumerableExtensions.ForEach<Person>(person => everyone.Add(person), employees, managers, customers);

			Assert.IsTrue(everyone[0] is Employee);
			Assert.IsTrue(everyone[1] is Manager);
			Assert.IsTrue(everyone[2] is Customer);
		}

		[Test]
		public void FindTest()
		{
			var letters = new ArrayList { "a", "b", "c" };
			var numbers = new ArrayList { 1, 2, 3 };

			Assert.AreEqual("b", letters.Find<string>(l => l == "b"));
			Assert.IsNull(letters.Find<string>(l => l == "z"));
			Assert.AreEqual(2, numbers.Find<int>(n => n == 2));
			Assert.AreEqual(0, numbers.Find<int>(n => n == 10));
		}

		[Test]
		public void AsEnumerableTest()
		{
			var people = new ArrayList
			{ 
				new Person { Name = "Bob" }, 
				new Person { Name = "Doug" },
				new Person { Name = "Claude" },
				new Person { Name = "Pam" }
			};

			Assert.AreSame(people[people.Count - 1], people.AsEnumerable<Person>().Last());
		}
	}
}
